package com.atguigu.aiproject.utils;

import com.atguigu.aiproject.dto.PublishPostDTO;
import com.atguigu.aiproject.dto.ReviewResultDTO;
import com.atguigu.aiproject.entity.Creation;
import com.atguigu.aiproject.entity.ReviewResult;
import com.atguigu.aiproject.service.impl.CategoryServiceImpl;
import com.atguigu.aiproject.vo.CreationDetailsVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MappingUtils {
    /**
     * 将ReviewResultDTO和PublishPostDTO映射为ReviewResult对象
     *
     * @param reviewResultDTO
     * @param publishPostDTO
     * @return
     */
    public static ReviewResult mappingReviewResult(ReviewResultDTO reviewResultDTO, PublishPostDTO publishPostDTO) {
        //处理reviewResultDTO
        ReviewResult reviewResult = new ReviewResult();
        reviewResult.setApproved(reviewResultDTO.isApproved());
        reviewResult.setReason(reviewResultDTO.getReason());
        reviewResult.setReviewType(String.valueOf(reviewResultDTO.getReviewType().name()));
        reviewResult.setRiskLevel(reviewResultDTO.getRiskLevel());
        // 将ReviewResultDTO中的violationTypes以逗号分割映射到ReviewResult的violationTypes字段
        if (reviewResultDTO.getViolationTypes() != null) {
            String violationTypesStr = String.join(",", reviewResultDTO.getViolationTypes());
            reviewResult.setViolationTypes(violationTypesStr);
        }
        // 同样处理suggestions字段
        if (reviewResultDTO.getSuggestions() != null) {
            String suggestionsStr = String.join(",", reviewResultDTO.getSuggestions());
            reviewResult.setSuggestions(suggestionsStr);
        }

        // 处理publishPostDTO
        reviewResult.setContentTitle(publishPostDTO.getTitle());
        reviewResult.setContentCategory(publishPostDTO.getCgyName());
        return reviewResult;
    }
    /**
     * 将PublishPostDTO映射为Creation对象
     *
     * @param publishPostDTO
     * @return
     */
    public static Creation mappingCreation(PublishPostDTO publishPostDTO){
        Creation creation = new Creation();
        creation.setTitle(publishPostDTO.getTitle());
        creation.setContent(publishPostDTO.getContent());
        creation.setKeyword(publishPostDTO.getKeyword());
        creation.setCgyName(publishPostDTO.getCgyName());
        creation.setUserId(publishPostDTO.getUserId());
        creation.setClick(0);
        creation.setUserId(publishPostDTO.getUserId());
        creation.setCoverImg(publishPostDTO.getCoverImg());
        String cgyName = creation.getCgyName();
        creation.setCgyId(CategoryServiceImpl.getCategoryId_s(cgyName));
        creation.setReviewStatus(2);//设置为已经审核过
        return creation;
    }

    /**
     * 将作品详情对象转换为Map
     */
    public static Map<String, Object> creationDetailToMap(CreationDetailsVO detail) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", detail.getSId());
        map.put("title", detail.getSTitle());
        map.put("content", detail.getSContent());
        map.put("authorId", detail.getUId());
        map.put("author", detail.getSUserNickName());
        map.put("avatar", detail.getUAvatar());
        map.put("category", detail.getSCgyName());
        map.put("keywords", detail.getSKeyword());
        map.put("liked", detail.getSLiked()); // 点赞数
        map.put("click", detail.getSClick()); // 浏览量
        map.put("coverImg", detail.getSCoverImg());
        map.put("img", detail.getSImg());
        map.put("createTime", detail.getSCreateTime());
        return map;
    }

    /**
     * 从Hash重建作品详情对象
     */
    public static CreationDetailsVO mapToCreationDetail(Map<Object, Object> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        CreationDetailsVO detail = new CreationDetailsVO();
        //数字类型默认转换为Long,需要转换
        detail.setSId((Integer) map.get("id"));
        detail.setSTitle((String) map.get("title"));
        detail.setSContent((String) map.get("content"));
        detail.setUId((Integer) map.get("authorId"));
        detail.setSUserNickName((String) map.get("author"));
        detail.setUAvatar((String) map.get("avatar"));
        detail.setSCgyName((String) map.get("category"));
        detail.setSKeyword((String) map.get("keywords"));
        detail.setSLiked((Integer) map.get("liked"));
        detail.setSClick((Integer) map.get("click"));
        detail.setSCoverImg((String) map.get("coverImg"));
        detail.setSImg((List<String>) map.get("img"));
        detail.setSCreateTime((String) map.get("createTime"));
        return detail;
    }
}
